/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.view.client;

import java.util.ArrayList;
import java.util.List;

import com.cleangwt.json.client.SimpleObject;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.view.client.CellPreviewEvent;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.TreeViewModel;

/**
 * The {@link TreeViewModel} for {@link SimpleObject}.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class SimpleTreeViewModel implements TreeViewModel {

  private List<String> childTypes = new ArrayList<String>();
  private List<Cell<SimpleObject>> cells = new ArrayList<Cell<SimpleObject>>();

  private ListDataProvider<SimpleObject> dataProvider;
  private CellPreviewEvent.Handler<SimpleObject> selectionManager;
  private SelectionModel<SimpleObject> selectionModel;
  private ValueUpdater<SimpleObject> valueUpdater;

  private List<List<SimpleObject>> parentNodes = new ArrayList<List<SimpleObject>>();

  public SimpleTreeViewModel(ListDataProvider<SimpleObject> dataProvider) {
    this.dataProvider = dataProvider;
  }

  /**
   * Adds a leaf node.
   * 
   * @param cell the cell
   */
  public void addNode(Cell<SimpleObject> cell) {
    assert cells.size() == childTypes.size() : "You may not add a node after leaf.";
    this.cells.add(cell);
    initParentNodes(0, this.dataProvider.getList());
  }

  /**
   * Adds a node with child type.
   * 
   * @param cell the cell
   * @param childType the child type used to retrieve the childs form layer value
   */
  public void addNode(Cell<SimpleObject> cell, String childType) {
    assert cells.size() == childTypes.size() : "You may not add a node after leaf.";
    this.cells.add(cell);
    this.childTypes.add(childType);
  }

  private void initParentNodes(int layer, List<SimpleObject> layerData) {
    if (layer < this.childTypes.size()) {
      if (layer >= parentNodes.size()) {
        this.parentNodes.add(new ArrayList<SimpleObject>(layerData));
      } else {
        parentNodes.get(layer).addAll(layerData);
      }
    }

    if (layer < this.childTypes.size()) {
      int next = layer + 1;
      for (SimpleObject v : layerData) {
        this.initParentNodes(next, v.<SimpleObject> getList(this.childTypes.get(layer)));
      }
    }
  }

  @Override
  public <T> NodeInfo<?> getNodeInfo(T value) {
    if (value == null) {
      return new DefaultNodeInfo<SimpleObject>(dataProvider, cells.get(0));
    } else {
      SimpleObject v = (SimpleObject) value;
      int layer = getLayer(v);
      List<SimpleObject> nodeData = v.getList(childTypes.get(layer));
      ListDataProvider<SimpleObject> provider = new ListDataProvider<SimpleObject>(nodeData);
      int next = layer + 1;
      if (next < childTypes.size()) {
        // nodes
        return new DefaultNodeInfo<SimpleObject>(provider, cells.get(next));
      } else {
        // leaf
        return new DefaultNodeInfo<SimpleObject>(provider, cells.get(next), selectionModel,
            selectionManager, getValueUpdater());
      }

    }

  }

  /**
   * Gets the layer of value.
   * 
   * @param value the value in the tree model
   * @return the layer
   */
  public int getLayer(SimpleObject value) {
    int layer = 0;
    for (List<SimpleObject> v : this.parentNodes) {
      if (v.contains(value)) {
        return layer;
      }
      layer++;
    }
    throw new IllegalArgumentException();
  }

  @Override
  public boolean isLeaf(Object value) {
    if (value == null) {
      return false;
    }
    for (List<SimpleObject> node : parentNodes) {
      if (node.contains(value)) {
        return false;
      }
    }
    return true;
  }

  /**
   * @return the selectionModel
   */
  public SelectionModel<SimpleObject> getSelectionModel() {
    return selectionModel;
  }

  /**
   * @param selectionModel the selectionModel to set
   */
  public void setSelectionModel(SelectionModel<SimpleObject> selectionModel) {
    this.selectionModel = selectionModel;
  }

  /**
   * @return the selectionManager
   */
  public CellPreviewEvent.Handler<SimpleObject> getSelectionManager() {
    return selectionManager;
  }

  /**
   * @param selectionManager the selectionManager to set
   */
  public void setSelectionManager(CellPreviewEvent.Handler<SimpleObject> selectionManager) {
    this.selectionManager = selectionManager;
  }

  /**
   * @return the valueUpdater
   */
  public ValueUpdater<SimpleObject> getValueUpdater() {
    return valueUpdater;
  }

  /**
   * @param valueUpdater the valueUpdater to set
   */
  public void setValueUpdater(ValueUpdater<SimpleObject> valueUpdater) {
    this.valueUpdater = valueUpdater;
  }

}
